import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

//导入组件
import Login from '../components/login.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'

Vue.use(VueRouter)

  const routes = [
    {//重定向redirect
      path:'/',redirect:'/login'
    },
    {
      path:"/login",
      component:Login
    },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect:'/welcome',//重定向到welcome
    children:[
    {path:'/welcome',component:Welcome},
    {path:'/users',component:Users},
    {path:'/rights',component:Rights},
    {path:'/roles',component:Roles}
  ]//添加子路径
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]

const router = new VueRouter({
  routes
})


//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
//to将要访问的路径
//from代表从哪个路径跳转而来
//next是一个函数，表示放行
// next() 放行 next('/login') 强制跳转
if(to.path==='/login')return next();//如果是登录页就放行
//获取sessionStorage存储的登录状况值
const loginStr=window.sessionStorage.getItem('isLogined')
if (!loginStr) return next('/login')
next()
//如果不是登录那么就会跳到登录界面
})

export default router
